/*
 * @lc app=leetcode.cn id=724 lang=golang
 *
 * [724] 寻找数组的中心下标
 */
package Solutions

// @lc code=start

func pivotIndex(nums []int) int {
	if len(nums) == 0 {
		return 0
	}
	for i := 1; i < len(nums); i++ {
		nums[i] += nums[i-1]
	}
	sum := nums[len(nums)-1]
	for i := 0; i < len(nums); i++ {
		var left = 0
		if i > 0 {
			left = nums[i-1]
		}
		if sum-nums[i] == left {
			return i
		}
	}
	return -1
}

// @lc code=end
